<?php
@header("Content-type: text/html; charset=utf-8");
session_start();
if (isset($_SESSION['openid']) && $_SESSION['openid'] == true) {
    $openid = $_SESSION['openid'];
} else {
    echo "<script>window.location='error.php';</script>";
}

include("../db.php");
include("../../wall/biaoqing.php");
$ppluserinfoc = new M('paipaile_user');
$ppluserinfo = $ppluserinfoc->find('openid="'.$openid.'"');
if (isset($_GET['do'])) {
    $do = $_GET['do'];
} else {
    die("invild action");
}

/**
 * 拍拍乐回复函数$pplconf['pplnum']为拍拍乐当前状态
 * $pplconf['pplnum']=1表示未上传
 * $pplconf['pplnum']=2表示上传中
 * $pplconf['pplnum']=3表示投票中
 * *$check[2]=4表示用户当前是否处于拍拍乐模式
 * $ppluserinfo['status']表示该用户的投票状态 0表示该用户未投票
 * $ppluserinfo['status']表示该用户的投票状态 1表示该用户已经投过票
 * * $ppluserinfo['status']表示该用户的投票状态 2表示该用户未投票，但已经上传过照片，不能再上传照片
 * */
switch ($do) {

    case "getinfo":
        getinfo();
        break;

    case "submit":
        submit();
        break;


}
function getinfo()
{
    global $xuanzezu, $ppluserinfo, $pplconf;
    if ($pplconf['pplnum'] != 3 || $ppluserinfo['status'] == 1) {
        echo json_encode('wait!');
        die;
    }
    $paipaile_m = new M('paipaile');
    $rows = $paipaile_m->select('id>0');
    // $sql_vote_check = "SELECT * FROM `weixin_paipaile` where id>0";
    // $query_vote_check = mysql_query($sql_vote_check);
    for ($i = 0, $l = count($rows); $i < $l; $i++) {
        $row1 = $rows[$i];
        $row1['nickname'] = pack('H*', $row1['nickname']);
        $row1 = emoji_unified_to_html(emoji_softbank_to_unified($row1));
        $row1['nickname'] = $row1['nickname'] . "(" . $row1['voteres'] . "票)";
        $ppl_check[] = $row1;
    }
    //      while($row1=mysql_fetch_array($query_vote_check)){

    // $row1['nickname']=pack('H*',$row1['nickname']);
    // $row1=emoji_unified_to_html(emoji_softbank_to_unified($row1));
    // $row1['nickname']=$row1['nickname']."(".$row1['voteres']."票)";
    //      $ppl_check[] = $row1;

    //  	}

    // var_dump($ppl_check);
    $ppl_check[0]['state'] = 3;
    echo json_encode($ppl_check);
}

function submit()
{
    global $xuanzezu, $openid, $ppluserinfo, $pplconf;
    $id = $_POST['voteid'];
    $paipaile_m = new M('paipaile');
    //$openid = $_SESSION['openid'];
    if (count($id) != $xuanzezu['votecannum']) {
        echo "sorry2";
        die;
    }
    // echo var_export($ppluserinfo);
    if ($ppluserinfo['status'] == 1 || empty($ppluserinfo)) {
        echo "sorry";
        die;
    }
    if ($pplconf['pplnum'] != 3) {
        echo "sorry3";
        die;
    }

    $idvalues = implode(",", $id);
    $sql_flag = "UPDATE  `weixin_paipaile_user` SET  `status` =  '1' WHERE `openid` = '{$openid}'";
    $paipaile_m->query($sql_flag);
    foreach ($id as $value) {
        $sql_vote = "UPDATE  `weixin_paipaile` SET  `voteres` =  `voteres`+1 WHERE `id` = '{$value}'";
        $paipaile_m->query($sql_vote);
    }
    echo "ok";
}
